package com.bci.pwtz.service.cad;

import java.util.ArrayList;

public class Cad_Rect
{
  public double bottom = 0.0D;
  public double left = 0.0D;
  protected int rectType = 3;
  Cad_Point pt1 = new Cad_Point();
  Cad_Point pt2 = new Cad_Point();
  public double right = 0.0D;
  public double top = 0.0D;

  public Cad_Rect()
  {
    this.top = 0.0D;
    this.bottom = 0.0D;
    this.right = 0.0D;
    this.left = 0.0D;
    this.pt1.clear();
    this.pt2.clear();
  }

  public Cad_Rect(double pDouble1, double pDouble2, double pDouble3, double pDouble4)
  {
    set(pDouble1, pDouble2, pDouble3, pDouble4);
  }

  public Cad_Rect(int type)
  {
    this.top = 0.0D;
    this.bottom = 0.0D;
    this.right = 0.0D;
    this.left = 0.0D;
    this.pt1.clear();
    this.pt2.clear();
    this.rectType = type;
  }

  public Cad_Rect(Cad_Point pPoint1, Cad_Point pPoint2)
  {
    set(pPoint1, pPoint2);
  }

  public Cad_Rect(Cad_Rect pRect)
  {
    set(pRect);
  }

  public static Cad_Point _getLLjc(Cad_Rect pRect1, Cad_Rect pRect2)
  {
    Cad_Point point1=null;
    if ((pRect1 == null) || (pRect2 == null))
      point1 = null;
    Cad_Point point2;
    Cad_Point point3;
    Cad_Point point4;
    Cad_Point point5;
    
    return point1;

//    do
//    {
//      return point1;
//      if ((double_equals(Cad_Line._getjl(pRect2), 0.0D)) || (double_equals(Cad_Line._getjl(pRect1), 0.0D)))
//      {
//        if (!double_equals(Cad_Line._getjl(pRect2), 0.0D))
//        {
//          if (_ptOnLine(pRect2, pRect1.pt1) == 0)
//            return null;
//          return pRect1.pt1;
//        }
//        if (!double_equals(Cad_Line._getjl(pRect1), 0.0D))
//        {
//          if (_ptOnLine(pRect1, pRect2.pt1) == 0)
//            return null;
//          return pRect2.pt1;
//        }
//      }
//      pRect2.rectType = 1;
//      pRect1.rectType = 1;
//      point2 = new Ex_dpoint(pRect1.pt1);
//      point3 = new Ex_dpoint(pRect1.pt2);
//      point1 = new Ex_dpoint(pRect2.pt1);
//      point4 = new Ex_dpoint(pRect2.pt2);
//      point5 = new Ex_dpoint();
//      double d1 = (point2.y - point3.y) / (point2.x - point3.x);
//      double d2 = (point1.y - point4.y) / (point1.x - point4.x);
//      if ((double_equals(pRect1.size().cx, 0.0D)) || (double_equals(pRect1.size().cy, 0.0D)))
//        d1 = 0.0D;
//      if ((double_equals(pRect2.size().cx, 0.0D)) || (double_equals(pRect2.size().cy, 0.0D)))
//        d2 = 0.0D;
//      if (double_equals(d1, d2))
//      {
//        if ((double_equals(pRect1.size().cx, 0.0D)) && (double_equals(pRect2.size().cx, 0.0D)) && (pRect1.pt1.y != pRect2.pt1.y))
//          return null;
//        if ((double_equals(pRect1.size().cy, 0.0D)) && (double_equals(pRect2.size().cy, 0.0D)) && (pRect1.pt1.x != pRect2.pt1.x))
//          return null;
//      }
//    }
//    while (_ptOnLine(pRect1, point1) != 0);
//    if (_ptOnLine(pRect1, point4) != 0)
//      return point4;
//    if (_ptOnLine(pRect2, point2) != 0)
//      return point2;
//    if (_ptOnLine(pRect2, point3) != 0)
//      return point3;
//    point5.x = (((point1.y * point4.x - point1.x * point4.y) * (point2.x - point3.x) - (point2.y * point3.x - point2.x * point3.y) * (point1.x - point4.x)) / ((point1.y - point4.y) * (point2.x - point3.x) - (point2.y - point3.y) * (point1.x - point4.x)));
//    double d3;
//    if (double_equals(point2.x, point3.x))
//      d3 = point5.x * (point1.y - point4.y) - point1.y * point4.x + point1.x * point4.y;
//    for (double d4 = point1.x - point4.x; ; d4 = point2.x - point3.x)
//    {
//      point5.y = (d3 / d4);
//      return point5;
//      d3 = point5.x * (point2.y - point3.y) - point2.y * point3.x + point2.x * point3.y;
//    }
  }

  public static Cad_Point _getLLsjc(Cad_Rect pRect1, Cad_Rect pRect2)
  {
		Cad_Point point;
		if ((pRect1 == null) || (pRect2 == null))
			point = null;
		
		point = _getLLjc(pRect1, pRect2);
		
		if (_ptOnLine(pRect1, point) == 1
			&& _ptOnLine(pRect2, point) == 1) {
			return point;
		}
		return null;

//    do
//    {
//      return point;
//      point = _getLLjc(pRect1, pRect2);
//    }
//    while ((_ptOnLine(pRect1, point) == 1) && (_ptOnLine(pRect2, point) == 1));
//    return null;
  }

  public static Cad_Point _getPLcz(Cad_Rect pRect, Cad_Point pPoint)
  {
    if ((pRect == null) || (pPoint == null))
      pPoint = null;
    
    if(_ptOnLine(pRect, pPoint) == 1)
      return pPoint;
    
    Cad_Point point1 = new Cad_Point();
    Cad_Point point2 = new Cad_Point();
    Cad_Point point3 = new Cad_Point();
    Cad_Point point4 = new Cad_Point();
    point2.copy(pRect.pt1);
    point3.copy(pRect.pt2);
    point4.copy(pPoint);
    if (point2.x == point3.x) {
      point1.x = point2.x;
      point1.y = point4.y;
    }
    
    if (point2.y == point3.y)
    {
    	point1.x = point4.x;
        point1.y = point2.y;
	}
	else
	{
        double d1 = Math.sqrt(Math.pow(point2.x - point3.x, 2.0D) + Math.pow(point2.y - point3.y, 2.0D));
        double d2 = (Math.pow(point3.x - point4.x, 2.0D) + Math.pow(point3.y - point4.y, 2.0D) + (Math.pow(point2.x - point3.x, 2.0D) + Math.pow(point2.y - point3.y, 2.0D)) - (Math.pow(point2.x - point4.x, 2.0D) + Math.pow(point2.y - point4.y, 2.0D))) / (2.0D * d1);
        point3.x -= d2 * (point3.x - point2.x) / d1;
        point3.y -= d2 * (point3.y - point2.y) / d1;
	}
	return point1;
  }

  public static Cad_Point _getPLscz(Cad_Rect pRect, Cad_Point pPoint)
  {
    Cad_Point point = _getPLcz(pRect, pPoint);
    if ((point != null) && (_ptOnLine(pRect, point) == 1))
      return point;
    return null;
  }

  public static int _getP_Ls_scz(ArrayList<Cad_Rect> pArrayList, Cad_Point pPoint)
  {
		int i = -1;
		if ((pArrayList == null) || (pPoint == null))
			return i;

		double d = 1000000.0D;
		i = -1;
		for (int j = 0; j < pArrayList.size(); j++) {
			Cad_Point point = _getPLscz((Cad_Rect) pArrayList.get(j),
					pPoint);
			if ((point != null)
					&& (d > Cad_Line._getjl(pPoint, point))) {
				d = Cad_Line._getjl(pPoint, point);
				i = j;
			}
		}
		return i;
  }

  public static Cad_Rect _getsamerc(Cad_Rect pRect1, Cad_Rect pRect2)  {
    Cad_Rect rect = new Cad_Rect();
    int i;
    Cad_Size localEx_dsize8;
    if (_ptInRect(pRect1, pRect2.lefttop()))
    {
      rect.pt1.x = pRect2.left;
      rect.pt2.y = pRect2.top;
      rect.pt2.x = Math.min(pRect1.right, pRect2.right);
      rect.pt1.y = Math.max(pRect1.bottom, pRect2.bottom);
      i = 1;
      localEx_dsize8 = rect.size();
      if (rect.size().cx < rect.size().cy)
      {
        rect.rectType = 2;
        rect.pt1.y = 0.0D;
        rect.pt1.x = localEx_dsize8.cx;
      }
      else{
        rect.rectType = 1;
        rect.pt1.x = 0.0D;
        rect.pt1.y = (-localEx_dsize8.cy);
      }
    }
//    while (i != 0)
//    {
//      return rect;
//      rect.rectType = 1;
//      rect.pt1.x = 0.0D;
//      rect.pt1.y = (-localEx_dsize8.cy);
//      continue;
//      if (_ptInRect(pRect1, pRect2.leftbottom())) 

    else if (_ptInRect(pRect1, pRect2.leftbottom()))
      {
        rect.pt1.x = pRect2.left;
        rect.pt1.y = pRect2.bottom;
        rect.pt2.x = Math.min(pRect1.right, pRect2.right);
        rect.pt2.y = Math.min(pRect1.top, pRect2.top);
        i = 1;
        Cad_Size localEx_dsize7 = rect.size();
        if (rect.size().cx < rect.size().cy)
        {
          rect.rectType = 2;
          rect.pt1.y = 0.0D;
          rect.pt1.x = localEx_dsize7.cx;
        }
        else
        {
          rect.rectType = 3;
          rect.pt1.x = 0.0D;
          rect.pt1.y = localEx_dsize7.cy;
        }
      }
      else if (_ptInRect(pRect1, pRect2.righttop()))
      {
        rect.pt2.x = pRect2.right;
        rect.pt2.y = pRect2.top;
        rect.pt1.x = Math.max(pRect1.left, pRect2.left);
        rect.pt1.y = Math.max(pRect1.bottom, pRect2.bottom);
        i = 1;
        Cad_Size localEx_dsize6 = rect.size();
        if (rect.size().cx < rect.size().cy)
        {
          rect.rectType = 0;
          rect.pt1.y = 0.0D;
          rect.pt1.x = (-localEx_dsize6.cx);
        }
        else
        {
          rect.rectType = 1;
          rect.pt1.x = 0.0D;
          rect.pt1.y = (-localEx_dsize6.cy);
        }
      }
      else if (_ptInRect(pRect1, pRect2.rightbottom()))
      {
        rect.pt2.x = pRect2.right;
        rect.pt1.y = pRect2.bottom;
        rect.pt1.x = Math.max(pRect1.left, pRect2.left);
        rect.pt2.y = Math.min(pRect1.top, pRect2.top);
        i = 1;
        Cad_Size localEx_dsize5 = rect.size();
        if (rect.size().cx < rect.size().cy)
        {
          rect.rectType = 2;
          rect.pt1.y = 0.0D;
          rect.pt1.x = localEx_dsize5.cx;
        }
        else
        {
          rect.rectType = 3;
          rect.pt1.x = 0.0D;
          rect.pt1.y = localEx_dsize5.cy;
        }
      }
      else if ((pRect1.left >= pRect2.left) && (pRect1.left <= pRect2.right) && (((pRect2.top <= pRect1.top) && (pRect2.bottom >= pRect1.bottom)) || ((pRect1.top <= pRect2.top) && (pRect1.bottom >= pRect2.bottom))))
      {
        rect.pt1.x = pRect1.left;
        rect.pt1.y = Math.max(pRect1.bottom, pRect2.bottom);
        rect.pt2.x = Math.min(pRect1.right, pRect2.right);
        rect.pt2.y = Math.min(pRect1.top, pRect2.top);
        i = 1;
        rect.rectType = 0;
        Cad_Size localEx_dsize4 = rect.size();
        rect.pt1.y = 0.0D;
        rect.pt1.x = (-localEx_dsize4.cx);
      }
      else if ((pRect2.top >= pRect1.bottom) && (pRect2.top <= pRect1.top) && (((pRect2.left <= pRect1.left) && (pRect2.right >= pRect1.right)) || ((pRect2.left >= pRect1.left) && (pRect2.right <= pRect1.right))))
      {
        rect.pt1.x = Math.max(pRect1.left, pRect2.left);
        rect.pt1.y = Math.max(pRect1.bottom, pRect2.bottom);
        rect.pt2.x = Math.min(pRect1.right, pRect2.right);
        rect.pt2.y = pRect2.top;
        i = 1;
        rect.rectType = 1;
        Cad_Size localEx_dsize3 = rect.size();
        rect.pt1.x = 0.0D;
        rect.pt1.y = (-localEx_dsize3.cy);
      }
      else if ((pRect2.left >= pRect1.left) && (pRect2.left <= pRect1.right) && (((pRect2.top <= pRect1.top) && (pRect2.bottom > pRect1.bottom)) || ((pRect2.top >= pRect1.top) && (pRect2.bottom <= pRect1.bottom))))
      {
        rect.pt1.x = pRect2.left;
        rect.pt1.y = Math.max(pRect1.bottom, pRect2.bottom);
        rect.pt2.x = Math.min(pRect1.right, pRect2.right);
        rect.pt2.y = Math.min(pRect1.top, pRect2.top);
        i = 1;
        rect.rectType = 2;
        Cad_Size localEx_dsize2 = rect.size();
        rect.pt1.y = 0.0D;
        rect.pt1.x = localEx_dsize2.cx;
      }
      else if ((pRect2.bottom >= pRect1.bottom) && (pRect2.bottom <= pRect1.top) && (((pRect2.left <= pRect1.left) && (pRect2.right >= pRect1.right)) || ((pRect2.left >= pRect1.left) && (pRect2.right <= pRect1.right))))
      {
        rect.pt1.x = Math.max(pRect1.left, pRect2.left);
        rect.pt1.y = pRect2.bottom;
        rect.pt2.x = Math.min(pRect1.right, pRect2.right);
        rect.pt2.y = Math.min(pRect1.top, pRect2.top);
        i = 1;
        rect.rectType = 3;
        Cad_Size localEx_dsize1 = rect.size();
        rect.pt1.x = 0.0D;
        rect.pt1.y = (pRect1.top + localEx_dsize1.cy);
      }
      else if ((pRect2.left > pRect1.left) || (pRect2.right < pRect1.right) || (pRect1.bottom > pRect2.bottom) || (pRect1.top < pRect2.top))
      {
        boolean bool1 = pRect1.left < pRect2.left;
        i = 0;
        if (!bool1)
        {
          boolean bool2 = pRect1.right < pRect2.right;
          i = 0;
          if (!bool2)
          {
            boolean bool3 = pRect2.bottom < pRect1.bottom;
            i = 0;
            if (!bool3)
            {
              boolean bool4 = pRect2.top < pRect1.top;
              i = 0;
              if (bool4);
            }
          }
        }
      }
      else
      {
        rect.pt1.x = Math.max(pRect1.left, pRect2.left);
        rect.pt1.y = Math.max(pRect1.bottom, pRect2.bottom);
        rect.pt2.x = Math.min(pRect1.right, pRect2.right);
        rect.pt2.y = Math.min(pRect1.top, pRect2.top);
        i = 1;
        double d1=0;
        if ((pRect2.left <= pRect1.left) && (pRect2.right >= pRect1.right) && (pRect1.bottom <= pRect2.bottom) && (pRect1.top >= pRect2.top)){
				d1 = Math.abs(pRect1.top - rect.top);
        }
        else{
				d1 = Math.abs(pRect2.top - rect.top);
        }
        double d2 = Math.abs(rect.bottom - pRect1.bottom);
        if (d1 < d2){
				rect.pt1.x = 0.0D;
				rect.pt1.y = (pRect1.bottom - pRect2.top);
				rect.rectType = 1;
        }
        else{
				rect.pt1.y = (pRect1.top - pRect2.bottom);
				rect.rectType = 3;
        }
//        for (double d2 = Math.abs(rect.bottom - pRect1.bottom); ; d2 = Math.abs(rect.bottom - pRect2.bottom))
//        {
//          if (d1 >= d2)
//            break label1670;
//          rect.pt1.x = 0.0D;
//          rect.pt1.y = (pRect1.bottom - pRect2.top);
//          rect.rectType = 1;
//          break;
//          d1 = Math.abs(pRect2.top - rect.top);
//        }
//        label1670: rect.pt1.x = 0.0D;
//        rect.pt1.y = (pRect1.top - pRect2.bottom);
//        rect.rectType = 3;
      }
    if  (i != 0)  {
    	return rect;
    }
    else {
        return null;
    }
  }

  public static boolean _liInRect(Cad_Rect pRect1, Cad_Rect pRect2)
  {
    if ((pRect1 == null) || (pRect2 == null))
      return false;
    pRect2.gfzb();
    pRect1.rectType = 1;
    pRect2.rectType = 3;
    Cad_Rect localEx_drect1 = new Cad_Rect(pRect2.left, pRect2.top, pRect2.right, pRect2.top);
    Cad_Rect localEx_drect2 = new Cad_Rect(pRect2.left, pRect2.top, pRect2.left, pRect2.bottom);
    Cad_Rect localEx_drect3 = new Cad_Rect(pRect2.left, pRect2.bottom, pRect2.right, pRect2.bottom);
    Cad_Rect localEx_drect4 = new Cad_Rect(pRect2.right, pRect2.top, pRect2.right, pRect2.bottom);
    Cad_Point point1 = _getLLjc(localEx_drect1, pRect1);
    if ((point1 != null) && (_ptOnLine(localEx_drect1, point1) == 1) && (_ptOnLine(pRect1, point1) == 1))
      return true;
    Cad_Point point2 = _getLLjc(localEx_drect2, pRect1);
    if ((point2 != null) && (_ptOnLine(localEx_drect2, point2) == 1) && (_ptOnLine(pRect1, point2) == 1))
      return true;
    Cad_Point point3 = _getLLjc(localEx_drect4, pRect1);
    if ((point3 != null) && (_ptOnLine(localEx_drect4, point3) == 1) && (_ptOnLine(pRect1, point3) == 1))
      return true;
    Cad_Point point4 = _getLLjc(localEx_drect3, pRect1);
    return (point4 != null) && (_ptOnLine(localEx_drect3, point4) == 1) && (_ptOnLine(pRect1, point4) == 1);
  }

  public static boolean _lkjc_line(Cad_Rect pRect1, Cad_Rect pRect2)
  {
	    if ((pRect1 == null) || (pRect2 == null))
	    	return false;
	    
	    Cad_Point point;
	    point = _getLLjc(pRect1, pRect2);
	    if (point == null)
	    	return false;
	    if (_ptOnLine(pRect2, point) != -1)
	    	return false;
		if (_ptOnLine(pRect1, point) == -1)
			_mp_line(pRect1, point);
	    
		return true;
			
//	if ((pRect1 == null) || (pRect2 == null));
//    Ex_dpoint point;
//    do
//    {
//      do
//      {
//        return false;
//        point = _getLLjc(pRect1, pRect2);
//      }
//      while (point == null);
//      if (_ptOnLine(pRect1, point) == -1)
//        _mp_line(pRect1, point);
//    }
//    while (_ptOnLine(pRect2, point) != -1);
//    _mp_line(pRect2, point);
//    return false;
  }

  public static boolean _mp_line(Cad_Rect pRect, Cad_Point pPoint)
  {
    if ((pRect == null) || (pPoint == null))
      return false;
    if (Cad_Line._getjl(pRect.pt1, pPoint) < Cad_Line._getjl(pRect.pt2, pPoint))
    {
      pRect.pt1.copy(pPoint);
      return false;
    }
    pRect.pt2.copy(pPoint);
    return false;
  }

  public static boolean _ptInRect(Cad_Rect pRect, Cad_Point pPoint)
  {
    if ((pRect == null) || (pPoint == null))
    	return false;

    if (((pPoint.x <= pRect.left) && (!double_equals(pPoint.x, pRect.left))) || ((pPoint.x >= pRect.right) && (!double_equals(pPoint.x, pRect.right))) || ((pPoint.y <= pRect.bottom) && (!double_equals(pPoint.y, pRect.bottom))) || ((pPoint.y >= pRect.top) && (!double_equals(pPoint.y, pRect.top))))
    	return false;
    else
        pRect.gfzb();
    return true;
    
//    if ((pRect == null) || (pPoint == null));
//    do
//    {
//      return false;
//      pRect.gfzb();
//    }
//    while (((pPoint.x <= pRect.left) && (!double_equals(pPoint.x, pRect.left))) || ((pPoint.x >= pRect.right) && (!double_equals(pPoint.x, pRect.right))) || ((pPoint.y <= pRect.bottom) && (!double_equals(pPoint.y, pRect.bottom))) || ((pPoint.y >= pRect.top) && (!double_equals(pPoint.y, pRect.top))));
//    return true;
  }

  public static int _ptOnLine(Cad_Rect pRect, Cad_Point pPoint)
  {
    if ((pRect == null) || (pPoint == null))
    	return 0;
    if (!double_equals((pPoint.y - pRect.pt1.y) / (pPoint.x - pRect.pt1.x) / ((pPoint.y - pRect.pt2.y) / (pPoint.x - pRect.pt2.x)), 1.0D))
    {
        if (!double_equals((pPoint.y - pRect.pt1.y) / (pPoint.x - pRect.pt1.x) / ((pPoint.y - pRect.pt2.y) / (pPoint.x - pRect.pt2.x)), 1.0D)){
        	return 0;
        }
        else{
            pRect.rectType = 1;
            pRect.gfzb();
            if ((pRect.pt1.equals(pPoint)) || (pRect.pt2.equals(pPoint)))
              return 1;
            if ((double_equals(pRect.pt1.x, pRect.pt2.x)) && (double_equals(pPoint.x, pRect.pt1.x)))
            {
              if (((pPoint.y > pRect.bottom) || (double_equals(pPoint.y, pRect.bottom))) && ((pPoint.y < pRect.top) || (double_equals(pPoint.y, pRect.top))))
                return 1;
              return -1;
            }
            if ((double_equals(pRect.pt1.y, pRect.pt2.y)) && (double_equals(pPoint.y, pRect.pt1.y)))
            {
              if (((pPoint.x > pRect.left) || (double_equals(pPoint.x, pRect.left))) && ((pPoint.x < pRect.right) || (double_equals(pPoint.x, pRect.right))))
                return 1;
              return -1;
            }
            if (((pPoint.x <= pRect.left) && (!double_equals(pPoint.x, pRect.left))) || ((pPoint.x >= pRect.right) && (!double_equals(pPoint.x, pRect.right))) || ((pPoint.y <= pRect.bottom) && (!double_equals(pPoint.y, pRect.bottom))) || ((pPoint.y >= pRect.top) && (!double_equals(pPoint.y, pRect.top))))
            	return 1;
            return 1;
        }
    }
    else{
    	return -1;
    }

//    if ((pRect == null) || (pPoint == null));
//    do
//    {
//      do
//      {
//        return 0;
//        pRect.rectType = 1;
//        pRect.gfzb();
//        if ((pRect.pt1.equals(pPoint)) || (pRect.pt2.equals(pPoint)))
//          return 1;
//        if ((double_equals(pRect.pt1.x, pRect.pt2.x)) && (double_equals(pPoint.x, pRect.pt1.x)))
//        {
//          if (((pPoint.y > pRect.bottom) || (double_equals(pPoint.y, pRect.bottom))) && ((pPoint.y < pRect.top) || (double_equals(pPoint.y, pRect.top))))
//            return 1;
//          return -1;
//        }
//        if ((double_equals(pRect.pt1.y, pRect.pt2.y)) && (double_equals(pPoint.y, pRect.pt1.y)))
//        {
//          if (((pPoint.x > pRect.left) || (double_equals(pPoint.x, pRect.left))) && ((pPoint.x < pRect.right) || (double_equals(pPoint.x, pRect.right))))
//            return 1;
//          return -1;
//        }
//        if (((pPoint.x <= pRect.left) && (!double_equals(pPoint.x, pRect.left))) || ((pPoint.x >= pRect.right) && (!double_equals(pPoint.x, pRect.right))) || ((pPoint.y <= pRect.bottom) && (!double_equals(pPoint.y, pRect.bottom))) || ((pPoint.y >= pRect.top) && (!double_equals(pPoint.y, pRect.top))))
//          break;
//      }
//      while (!double_equals((pPoint.y - pRect.pt1.y) / (pPoint.x - pRect.pt1.x) / ((pPoint.y - pRect.pt2.y) / (pPoint.x - pRect.pt2.x)), 1.0D));
//      return 1;
//    }
//    while (!double_equals((pPoint.y - pRect.pt1.y) / (pPoint.x - pRect.pt1.x) / ((pPoint.y - pRect.pt2.y) / (pPoint.x - pRect.pt2.x)), 1.0D));
//    return -1;
  }

//  public static boolean _pt_line_czx(Cad_Point pPoint, Cad_Rect pRect, ArrayList<Cad_Rect> pArrayList)
//  {
//    Cad_Point point = _getPLcz(pRect, pPoint);
//    Cad_Rect rect = new Cad_Rect();
//    Cad_BaseInfo.getDeviceRect(9, rect, pPoint);
//    pArrayList.add(new Cad_Rect(getRectangLinkPt(rect, point), point));
//    if (_ptOnLine(pRect, pPoint) == -1)
//    {
//      _mp_line(pRect, point);
//      return true;
//    }
//    return false;
//  }

  public static double _pt_line_jl(Cad_Point pPoint, Cad_Rect pRect)
  {
    if ((pPoint == null) || (pRect == null))
      return -1.0D;
    return Cad_Line._getjl(pPoint, _getPLcz(pRect, pPoint));
  }

  public static int _pt_lines_nearline(Cad_Point pPoint, ArrayList<Cad_Rect> pArrayList)
  {
		int i=-1;
		if ((pPoint == null) || (pArrayList == null)){
			return i;
		}

		double d1 = 100000000.0D;
		for (int j = 0; j < pArrayList.size(); j++) {
			double d2 = Cad_Line._getjl(
					Cad_Line._pt_line_nearpt(pPoint,
							(Cad_Rect) pArrayList.get(j)), pPoint);
			if (d1 > d2) {
				d1 = d2;
				i = j;
				break;
			}
		}
		return i;
		//    while (true)
//    {
//      return i;
//      double d1 = 100000000.0D;
//      i = -1;
//      for (int j = 0; j < pArrayList.size(); j++)
//      {
//        double d2 = Cad_Line._getjl(Cad_Line._pt_line_nearpt(pPoint, (Ex_drect)pArrayList.get(j)), pPoint);
//        if (d1 > d2)
//        {
//          d1 = d2;
//          i = j;
//        }
//      }
//    }
  }

  public static Cad_Point _pt_lines_nearpt(Cad_Point pPoint, ArrayList<ArrayList<Cad_Rect>> pArrayList)
  {
		Cad_Point localObject = null;
		double d1 = 100000000.0D;

		for (int i = 0; i < pArrayList.size(); i++) {
			ArrayList<Cad_Rect> localArrayList = pArrayList.get(i);
			for (int j = 0; j < localArrayList.size(); j++) {
				Cad_Point point = Cad_Line._pt_line_nearpt(pPoint,
						(Cad_Rect) localArrayList.get(j));
				double d2 = Cad_Line._getjl(point, pPoint);
				if (d1 > d2) {
					d1 = d2;
					localObject = point;
					break;
				}
			}
		}
		return localObject;
    
//    int i = 0;
//    if (i >= pArrayList.size())
//      return localObject;
//    ArrayList localArrayList = (ArrayList)pArrayList.get(i);
//    for (int j = 0; ; j++)
//    {
//      if (j >= localArrayList.size())
//      {
//        i++;
//        break;
//      }
//      Ex_dpoint point = Cad_Line._pt_line_nearpt(pPoint, (Ex_drect)localArrayList.get(j));
//      double d2 = Cad_Line._getjl(point, pPoint);
//      if (d1 > d2)
//      {
//        d1 = d2;
//        localObject = point;
//      }
//    }
  }

  public static int _pt_near_line(Cad_Point pPoint, ArrayList<Cad_Rect> pArrayList, boolean pBoolean)
  {
    double d1 = 1000000.0D;
    double d2 = 1000000.0D;

    int i = -1;

    Cad_Point point;

    for (int j=0; j<pArrayList.size(); j++){
        if (pBoolean)
        {
        	point = _getPLscz((Cad_Rect)pArrayList.get(j), pPoint);
        	if (point == null){
        		continue;
        	}
      		d2 = Cad_Line._getjl(point, pPoint);
        }
        else{
            d2 = _pt_line_jl(pPoint, (Cad_Rect)pArrayList.get(j));
        }

        if (d2 < d1) {
            i = j;
            break;
        }
        d1 = d2;
    }
    return i;
    
//    int i = -1;
//    int j = 0;
//    if (j >= pArrayList.size())
//      return i;
//    Ex_dpoint point;
//    if (pBoolean)
//    {
//      point = _getPLscz((Ex_drect)pArrayList.get(j), pPoint);
//      if (point != null);
//    }
//    label103: 
//    while (true)
//    {
//      j++;
//      break;
//      for (double d2 = Cad_Line._getjl(point, pPoint); ; d2 = _pt_line_jl(pPoint, (Ex_drect)pArrayList.get(j)))
//      {
//        if (d2 >= d1)
//          break label103;
//        d1 = d2;
//        ((Ex_drect)pArrayList.get(j));
//        i = j;
//        break;
//      }
//    }
  }

  public static boolean double_equals(double pDouble1, double pDouble2)
  {
    return Math.abs(pDouble1 - pDouble2) < 1.E-009D;
  }

  public static Cad_Point getCircleLinkPt(Cad_Rect pRect, Cad_Point pPoint)
  {
    if ((pRect == null) || (pPoint == null))
      return null;
    return Cad_Line._getpoint(new Cad_Rect(pRect.pt1, pPoint), pRect.pt1, pRect.pt2.x);
  }

  public static Cad_Point getCircleLinkPt(Cad_Rect pRect1, Cad_Rect pRect2)
  {
    if ((pRect1 == null) || (pRect2 == null))
      return null;
    return getCircleLinkPt(pRect1, _getPLcz(pRect2, pRect1.pt1));
  }

  public static Cad_Point getLineLinkPt(Cad_Rect pRect, Cad_Point pPoint)
  {
    if ((pRect == null) || (pPoint == null))
      return null;
    return _getPLcz(pRect, pPoint);
  }

  public static Cad_Point getRectangLinkPt(Cad_Rect pRect, Cad_Point pPoint)
  {
    Cad_Point point;
    if ((pRect == null) || (pPoint == null))
      point = null;
    Cad_Rect localEx_drect1;
    Cad_Rect localEx_drect4;
    Cad_Rect localEx_drect5;
    Cad_Rect localEx_drect3;
    Cad_Rect localEx_drect2;

    pRect.gfzb();
    if (_ptInRect(pRect, pPoint))
      return pRect.center();
    
    localEx_drect1 = new Cad_Rect(pRect.center(), pPoint);
    localEx_drect2 = new Cad_Rect(pRect.left, pRect.top, pRect.right, pRect.top);
    localEx_drect3 = new Cad_Rect(pRect.left, pRect.top, pRect.left, pRect.bottom);
    localEx_drect4 = new Cad_Rect(pRect.left, pRect.bottom, pRect.right, pRect.bottom);
    localEx_drect5 = new Cad_Rect(pRect.right, pRect.top, pRect.right, pRect.bottom);
    point = _getLLjc(localEx_drect2, localEx_drect1);

    if ((point != null) && (_ptOnLine(localEx_drect2, point) == 1) && (_ptOnLine(localEx_drect1, point) == 1)){
    	return point;
    }
    else{
    	point = _getLLjc(localEx_drect3, localEx_drect1);
    }
    
    if ((point != null) && (_ptOnLine(localEx_drect3, point) == 1) && (_ptOnLine(localEx_drect1, point) == 1)) {
    	return point;
    }
    else{
    	point = _getLLjc(localEx_drect5, localEx_drect1);
    }
    if ((point != null) && (_ptOnLine(localEx_drect5, point) == 1) && (_ptOnLine(localEx_drect1, point) == 1)){
    	return point;
    }
    else {
    	point = _getLLjc(localEx_drect4, localEx_drect1);
    }
    if ((point == null) || (_ptOnLine(localEx_drect4, point) != 1) || (_ptOnLine(localEx_drect1, point) != 1)) {
    	return point;
    }
    
    return point;
    
//    Ex_dpoint point;
//    if ((pRect == null) || (pPoint == null))
//      point = null;
//    Ex_drect localEx_drect1;
//    Ex_drect localEx_drect4;
//    do
//    {
//      Ex_drect localEx_drect5;
//      do
//      {
//        Ex_drect localEx_drect3;
//        do
//        {
//          Ex_drect localEx_drect2;
//          do
//          {
//            return point;
//            pRect.gfzb();
//            if (_ptInRect(pRect, pPoint))
//              return pRect.center();
//            localEx_drect1 = new Ex_drect(pRect.center(), pPoint);
//            localEx_drect2 = new Ex_drect(pRect.left, pRect.top, pRect.right, pRect.top);
//            localEx_drect3 = new Ex_drect(pRect.left, pRect.top, pRect.left, pRect.bottom);
//            localEx_drect4 = new Ex_drect(pRect.left, pRect.bottom, pRect.right, pRect.bottom);
//            localEx_drect5 = new Ex_drect(pRect.right, pRect.top, pRect.right, pRect.bottom);
//            point = _getLLjc(localEx_drect2, localEx_drect1);
//          }
//          while ((point != null) && (_ptOnLine(localEx_drect2, point) == 1) && (_ptOnLine(localEx_drect1, point) == 1));
//          point = _getLLjc(localEx_drect3, localEx_drect1);
//        }
//        while ((point != null) && (_ptOnLine(localEx_drect3, point) == 1) && (_ptOnLine(localEx_drect1, point) == 1));
//        point = _getLLjc(localEx_drect5, localEx_drect1);
//      }
//      while ((point != null) && (_ptOnLine(localEx_drect5, point) == 1) && (_ptOnLine(localEx_drect1, point) == 1));
//      point = _getLLjc(localEx_drect4, localEx_drect1);
//    }
//    while ((point == null) || (_ptOnLine(localEx_drect4, point) != 1) || (_ptOnLine(localEx_drect1, point) != 1));
//    return point;
   }

  public static Cad_Point getRectangLinkPt(Cad_Rect pRect1, Cad_Rect pRect2)
  {
    if ((pRect1 == null) || (pRect2 == null))
      return null;
    return getRectangLinkPt(pRect1, _getPLcz(pRect2, pRect1.center()));
  }

  public void Exrc(double pDouble)
  {
    Cad_Point point6;
    if (this.pt1.x < this.pt2.x)
    {
      Cad_Point point7 = this.pt1;
      point7.x -= pDouble;
      Cad_Point point8 = this.pt2;
      point8.x = (pDouble + point8.x);
      if (this.pt1.y < this.pt2.y) {
          Cad_Point point5 = this.pt1;
          point5.y -= pDouble;
          point6 = this.pt2;
      }
    }
    
    Cad_Point point4;
    
    gfzb();
    return;
    
//    for (point6.y = (pDouble + point6.y); ; point4.y -= pDouble)
//    {
//      gfzb();
//      return;
//      Ex_dpoint point1 = this.pt2;
//      point1.x -= pDouble;
//      Ex_dpoint point2 = this.pt1;
//      point2.x = (pDouble + point2.x);
//      break;
//      Ex_dpoint point3 = this.pt1;
//      point3.y = (pDouble + point3.y);
//      point4 = this.pt2;
//    }
  }

  public Cad_Point bottomcen()
  {
    Cad_Point point = new Cad_Point();
    point.x = ((this.right - this.left) / 2.0D + this.left);
    point.y = this.bottom;
    return point;
  }

  public Cad_Point center()
  {
    if (this.rectType == 3)
    {
      Cad_Point point = new Cad_Point();
      point.set(this.left + size().cx / 2.0D, this.bottom + size().cy / 2.0D);
      return point;
    }
    if (this.rectType == 2)
      return this.pt1;
    if (this.rectType == 1)
      return Cad_Line._getpoint(this, this.pt1, linejl() / 2.0D);
    return this.pt1;
  }

  public boolean equals(Object pObject)
  {
    if (pObject == null)
      return false;
    if ((pObject instanceof Cad_Rect))
      return (this.pt1.equals(((Cad_Rect)pObject).pt1)) && (this.pt2.equals(((Cad_Rect)pObject).pt2));
    return false;
  }

  public Cad_Point getlinkpt(Cad_Point pPoint)
  {
    if (pPoint == null)
      return center();
    if (this.rectType == 0)
      return this.pt1;
    if (this.rectType == 1)
      return getLineLinkPt(this, pPoint);
    if (this.rectType == 2)
      return getCircleLinkPt(this, pPoint);
    if (this.rectType == 3)
      return getRectangLinkPt(this, pPoint);
    return this.pt1;
  }

  //gfzb = 规范坐标 ？
  public void gfzb()
  {
    switch (this.rectType)
    {
    case 0: //表示以pt1为准，矩形为1个点
//      double d1 = this.pt1.x;
//      this.right = d1;
//      this.left = d1;
//      double d2 = this.pt1.y;
//      this.bottom = d2;
//      this.top = d2;
      this.right = this.pt1.x;
      this.left = this.pt1.x;
      this.bottom = this.pt1.y;
      this.top = this.pt1.y;
      break;
    case 2: //pt1,pt2经过移位后的矩形
      this.left = (this.pt1.x - this.pt2.x);
      this.right = (this.pt1.x + this.pt2.x);
      this.bottom = (this.pt1.y - this.pt2.x);
      this.top = (this.pt1.y + this.pt2.x);
      break;
    case 1:
    case 3:
    	//表示以pt1,pt2为对角的矩形
        this.left = Math.min(this.pt1.x, this.pt2.x);
        this.right = Math.max(this.pt1.x, this.pt2.x);
        this.bottom = Math.min(this.pt1.y, this.pt2.y);
        this.top = Math.max(this.pt1.y, this.pt2.y);
        break;
    default:
        break;
    }
  }

  public Cad_Point leftbottom()
  {
    Cad_Point point = new Cad_Point();
    gfzb();
    point.x = this.left;
    point.y = this.bottom;
    return point;
  }

  public Cad_Point leftcen()
  {
    Cad_Point point = new Cad_Point();
    point.x = this.left;
    point.y = ((this.top - this.bottom) / 2.0D + this.bottom);
    return point;
  }

  public Cad_Point lefttop()
  {
    Cad_Point point = new Cad_Point();
    gfzb();
    point.x = this.left;
    point.y = this.top;
    return point;
  }

  public double linejl()
  {
    return Cad_Line._getjl(this.pt1, this.pt2);
  }

  public void moveof(double x, double y)
  {
    //Cad_Point point1 = this.pt1;
    //point1.x = (x + point1.x);
    this.pt1.x += x;
    
    //Cad_Point point2 = this.pt1;
    //point2.y = (y + point2.y);
    this.pt1.y += y;
    
    //Cad_Point point3 = this.pt2;
    //point3.y = (y + point3.y);
    this.pt2.y += y;
    
    if (this.rectType != 2) //2表示什么类型？
    {
      //Cad_Point point4 = this.pt2;
      //point4.x = (x + point4.x);
      this.pt2.x += x;
    }
    
    gfzb();
//    while (true)
//    {
//      gfzb();
//      return;
//      gfzb();
//    }
  }

  public void moveof(Cad_Point pPoint)
  {
    moveof(pPoint.x, pPoint.y);
  }

  public Cad_Point rightbottom()
  {
    Cad_Point point = new Cad_Point();
    gfzb();
    point.x = this.right;
    point.y = this.bottom;
    return point;
  }

  public Cad_Point rightcen()
  {
    Cad_Point point = new Cad_Point();
    point.x = this.right;
    point.y = ((this.top - this.bottom) / 2.0D + this.bottom);
    return point;
  }

  public Cad_Point righttop()
  {
    Cad_Point point = new Cad_Point();
    gfzb();
    point.x = this.right;
    point.y = this.top;
    return point;
  }

  public void set(double x1, double y1, double x2, double y2)
  {
    this.pt1.x = x1;
    this.pt1.y = y1;
    this.pt2.x = x2;
    this.pt2.y = y2;
    gfzb();
  }

  public void set(Cad_Point pPoint)
  {
    this.rectType = 0;
    this.pt1.copy(pPoint);
    this.pt2.copy(pPoint);
    gfzb();
  }

  public void set(Cad_Point pPoint, double pDouble)
  {
    this.rectType = 2;
    this.pt1.copy(pPoint);
    this.pt2.x = pDouble;
    gfzb();
  }

  public void set(Cad_Point pPoint1, Cad_Point pPoint2)
  {
    this.pt1.copy(pPoint1);
    this.pt2.copy(pPoint2);
    gfzb();
  }

  public void set(Cad_Point pPoint1, Cad_Point pPoint2, boolean pBoolean)
  {
    if (pBoolean)
      this.rectType = 1;
    set(pPoint1, pPoint2);
  }

  public void set(Cad_Rect pRect)
  {
    this.pt1.copy(pRect.pt1);
    this.pt2.copy(pRect.pt2);
    this.rectType = pRect.rectType;
    gfzb();
  }

  public Cad_Size size()
  {
    Cad_Size size = new Cad_Size();
    gfzb();
    size.cx = (this.right - this.left);
    size.cy = (this.top - this.bottom);
    return size;
  }

  public Cad_Point topcen()
  {
    Cad_Point point = new Cad_Point();
    point.x = ((this.right - this.left) / 2.0D + this.left);
    point.y = this.top;
    return point;
  }
}
